/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ftpultility;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import javax.swing.JOptionPane;

/**
 *
 * @author gilmario
 */
public class ConfiguracaoFtp implements Serializable {

    private static final long serialVersionUID = 4L;
    private static final int decode = 1818;
    private char[] usuario;
    private char[] servidor;
    private char[] senha;
    private char[] localR;
    private GilCripto gr;

    public void restauraCofiguracao() {
        try {
            File file = new File("conf/config.conf");
            if (file.exists()) {
                FileInputStream fis = new FileInputStream(file);
                ObjectInputStream ois = new ObjectInputStream(fis);
                ConfiguracaoFtp c = (ConfiguracaoFtp) ois.readObject();
                ois.close();
                setServidor(c.getServidor());
                setSenha(c.getSenha());
                setUsuario(c.getUsuario());
                setLocalR(c.getLocalR());
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e, "Erro no sistema", JOptionPane.ERROR_MESSAGE);
        }
    }

    public void salvaConfiguracao() {
        try {
            File file = new File("conf/config.conf");
            if (file.exists()) {
                file.renameTo(new File("conf/~config.conf"));
            } else {
                new File("conf").mkdir();
            }

            FileOutputStream fos = new FileOutputStream(file);
            ObjectOutputStream op = new ObjectOutputStream(fos);
            op.writeObject(this);
            op.close();
            JOptionPane.showMessageDialog(null, "Configuração salva");
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e, "Erro no sistema", JOptionPane.ERROR_MESSAGE);
        }
    }

    public ConfiguracaoFtp() {
        gr = new GilCripto();
    }

    public String getSenha() {
        return gr.DesCriptografar(senha, decode);
    }

    public void setSenha(String senha) {
        this.senha = gr.Criptografar(senha, decode);
    }

    public String getServidor() {
        return gr.DesCriptografar(servidor, decode);
    }

    public void setServidor(String servidor) {
        this.servidor = gr.Criptografar(servidor, decode);
    }

    public String getUsuario() {
        return gr.DesCriptografar(usuario, decode);
    }

    public void setUsuario(String usuario) {
        this.usuario = gr.Criptografar(usuario, decode);
    }

    public void setLocalR(String text) {
        this.localR = gr.Criptografar(text, decode);
    }

    public String getLocalR() {
        return gr.DesCriptografar(localR, decode);
    }
}
